package com.salinity.kun.other;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.salinity.kun.helper.HarmonicHelper;
import com.salinity.kun.helper.MatrixHelper;

import Jama.Matrix;

public class Li {

	public static void main(String[] args) {

		Path inputPath = Paths.get("E:\\Kuner\\work\\Li\\data.csv");

		Li li = new Li();
		try {
			List<double[]> paramsList = li.getParams(inputPath);

			List<double[][]> m = li.getMatrix(paramsList);

			HarmonicHelper hh = new HarmonicHelper();
	
			
			 double div = m.get(0)[0][0];
			 
			 
				Matrix A = new Matrix(m.get(0));
				Matrix Ab = new Matrix(m.get(1));

				Ab = Ab.times(1f / div);
				A = A.times(1f /div);
			 
			double[][] ma = A.getArray();
			
			double[][] mb =  Ab.getArray();
			
//
//			MatrixHelper.printMatrix(ma);
//			MatrixHelper.printMatrix(mb);
			
			double maxNum = 1;
			double offset = 0.0;
			double divCount = 20;
			double dt = maxNum/divCount;
			
			for(int i=0;i<divCount;i++) {
				Matrix a =  hh.getMatrixRslt(ma, mb, offset);
				 
				MatrixHelper.printMatrix(a.transpose().getArray(),"\t");
				offset+=dt;
			}
			
		
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<double[][]> getMatrix(List<double[]> paramsList) {

		int r = 4;

		double[][] ma = new double[r][r];
		double[][] mb = new double[r][1];
		List<double[][]> rsltList = new ArrayList<>();
		int len = paramsList.size();
		int i = 0, j = 0, k = 0;
		double[] params = null;
		for (; i < len; i++) {
			params = paramsList.get(i);
		
//			ma[0][0]+=params[0]*params[0];
//			ma[1][1]+=params[1]*params[1];
//			ma[2][2]+=params[2]*params[2];
//			ma[3][3]+=params[3]*params[3];
//			
//			ma[0][1]+=params[0]*params[1];
//			ma[0][2]+=params[0]*params[2];
//			ma[0][3]+=params[0]*params[3];
//
//			ma[1][0]+=params[1]*params[0];
//			ma[1][2]+=params[1]*params[2];
//			ma[1][3]+=params[1]*params[3];
			
			for(j=0;j<r;j++) {
				mb[j][0]+=params[j]*params[r];
				
				for(k=0;k<r;k++) {
					ma[j][k] += params[j]*params[k];	
				}
			}

		}

		rsltList.add(ma);
		rsltList.add(mb);

		
		return rsltList;

	}

	public List<double[]> getParams(Path inputPath) throws IOException {

		List<double[]> paramsList = new ArrayList<>();

		try (BufferedReader br = Files.newBufferedReader(inputPath)) {

			br.readLine();
			String line = "";
			String[] strArr = null;
			double[] tempDoubleArr;
			int i = 0;
			while ((line = br.readLine()) != null) {

				strArr = line.split(",");

				if (strArr.length == 5) {

					tempDoubleArr = new double[5];

					for (i = 0; i < 5; i++) {
						tempDoubleArr[i] = Double.parseDouble(strArr[i]);
					}
					paramsList.add(tempDoubleArr);

				} else {
					throw new RuntimeException("length != 5");
				}

			}

		}

		return paramsList;

	}

}
